<?php
// +----------------------------------------------------------------------
// | 后台首页
// +----------------------------------------------------------------------
// | Author: 李乾坤 <261001642@qq.com>
// +----------------------------------------------------------------------

class IndexAction extends CommonAction {
	// 首页
	public function index() {
		$this->display();
	}
	// 首页
	public function main() {
		// 统计数据
        $User=M("User");
        $lcheng=strtotime(date("Y-m-d")." 00:00:00");
        $info = array(
            '总会员数'=>$User->count("id")."个",
            '今日注册'=>$User->where("create_time>".$lcheng)->count("id")."个",
            '运行环境'=>PHP_OS ." ".$_SERVER["SERVER_SOFTWARE"],
            'PHP运行方式'=>php_sapi_name(),
            'ThinkPHP版本'=>THINK_VERSION.' [ <a href="http://thinkphp.cn" target="_blank">查看最新版本</a> ]',
            '上传附件限制'=>ini_get('upload_max_filesize'),
            '执行时间限制'=>ini_get('max_execution_time').'秒',
            '服务器时间'=>date("Y年n月j日 H:i:s"),
            '北京时间'=>gmdate("Y年n月j日 H:i:s",time()+8*3600),
            '服务器域名/IP'=>$_SERVER['SERVER_NAME'].' [ '.gethostbyname($_SERVER['SERVER_NAME']).' ]',
            '剩余空间'=>round((disk_free_space(".")/(1024*1024)),2).'M',
            'register_globals'=>get_cfg_var("register_globals")=="1" ? "ON" : "OFF",
            'magic_quotes_gpc'=>(1===get_magic_quotes_gpc())?'YES':'NO',
            'magic_quotes_runtime'=>(1===get_magic_quotes_runtime())?'YES':'NO',
            );
        $this->assign('info',$info);
		$this->display();
	}
    //搜索
    public function search(){
        $q=$this->_POST('q');
        $AuthRule=M('AuthRule');
        $this->list=$AccessList=$AuthRule->where('is_menu=1 and is_show=1 and status=1 and ((title like "%'.$q.'%") or (name like "%'.$q.'%"))')->Field('group_id,name,title')->order('sort asc')->select();
        $this->display();
    }
    function getMenu($uid=null,$fresh=false){
        $uid= is_null($uid) ? $_SESSION[C('USER_AUTH_KEY')] : $uid;
        if (!isset($_SESSION['menu'.$uid]) || $fresh){
            $Group=M('Group');
            $AuthRule=M('AuthRule');
            $AccessList=$AuthRule->where('is_menu=1 and is_show=1 and status=1')->Field('group_id,name,title')->order('sort asc')->select();
            $Menu=$Group->where('status=1')->getField('id,title,icon');
            import('ORG.Util.Auth');
            $auth=new Auth();
            $Menu['999999999']=array(
                    'id'=>'999999999',
                    'icon'=>'th',
                    'title'=>'其他',
            );

            if($_SESSION[C('ADMIN_AUTH_KEY')]){
                foreach ($AccessList as $value) {
                    if ($value['group_id']==0){
                        $Menu['999999999']['child'][]=$value;
                    }else{
                        $Menu[$value['group_id']]['child'][]=$value;
                    }
                }
            }else{
                foreach ($AccessList as $value) {
                    if(false===$auth->check($value['name'],$uid)){
                        continue;
                    }
                    if ($value['group_id']==0){
                        $Menu['999999999']['child'][]=$value;
                    }else{
                        $Menu[$value['group_id']]['child'][]=$value;
                    }
                }
            }
            $_SESSION['menu'.$uid]=$Menu;
        }
        return $_SESSION['menu'.$uid];
    }
    function menu(){
        if(C('AUTH_CONFIG.AUTH_TYPE')==1){
            $MenuList=$this->getMenu($_SESSION[C('USER_AUTH_KEY')],true);
        }else{
            $MenuList=$this->getMenu($_SESSION[C('USER_AUTH_KEY')]);
        }
        $tag=$_GET['tag'];
        $Menu=$MenuList[$tag];
        if(count($Menu['child'])==0 && count($Menu['999999999'])!=0 ){
            $Menu=$Menu['999999999'];
        }
        $this->assign('MenuList',$Menu);
        C('SHOW_RUN_TIME',false);           // 运行时间显示
        C('SHOW_PAGE_TRACE',false);
        $this->display();
    }
    public function profile() {
        $User    =   M("User");
        $vo =   $User->getById($_SESSION[C('USER_AUTH_KEY')]);
        $this->assign('vo',$vo);
        $this->display();
    }

    // 修改资料
    public function change() {
        $User    =   D("User");
        if(!$User->create()) {
            $this->error($User->getError());
        }
        $result =   $User->save();
        if(false !== $result) {
            $this->success('资料修改成功！');
        }else{
            $this->error('资料修改失败!');
        }
    }
    // 更换密码
    public function changePwd()
    {
        if (IS_POST){
            //对表单提交处理进行处理或者增加非表单数据
            /*if(md5(strtoupper($_POST['verify']))  != $_SESSION['verify']) {
                $this->error('验证码错误！');
            }*/
            $map    =   array();
            $map['password']= md5($_POST['password']);
            /*if(isset($_POST['account'])) {
                $map['account']  =   $_POST['account'];
            }elseif(isset($_SESSION[C('USER_AUTH_KEY')])) {*/
                $map['id']      =   $_SESSION[C('USER_AUTH_KEY')];
            //}
            //检查用户
            $User    =   M("User");
            if(!$User->where($map)->field('id')->find()) {
                $this->error('旧密码不符或者用户名错误！');
            }else {
                if (md5($_POST['newpassword']) !=md5($_POST['repassword'])){
                    $this->error('旧密码不符或者用户名错误！');
                }else{
                    $User->password =   md5($_POST['newpassword']);
                    $User->save();
                    $this->assign('jumpUrl','?s=/Index/index');
                    $this->success('密码修改成功！');
                }
            }
        }else{
            $User    =   M("User");
            $vo =   $User->getById($_SESSION[C('USER_AUTH_KEY')]);
            $this->assign('vo',$vo);
            $this->display();
        }
    }
}
?>